/* eslint-disable */
// eslint-disable-next-line
import { MrxDbgUiPrPoint, MxFun, MxDbCircleShape } from "mxdraw";

// 圆类继承自 MxDbCircleShape
class MxDbCircle extends MxDbCircleShape {
  constructor() {
    super();
    this.isClosedToCenter = false;
  }

  // 返回自定义对象的夹点
  getGripPoints() {
    const { x, y, z } = this.center;
    const upPoint = new THREE.Vector3(x, y + this.radius, z);
    const downPoint = new THREE.Vector3(x, y - this.radius, z);
    const leftPoint = new THREE.Vector3(x - this.radius, y, z);
    const rightPoint = new THREE.Vector3(x + this.radius, y, z);
    return [this.center, upPoint, downPoint, leftPoint, rightPoint];
  }

  // 移动夹点
  moveGripPointsAt(index, offset) {
    const [center, upPoint, downPoint, leftPoint, rightPoint] = this.getGripPoints();
    if (index === 0) this.center = center.add(offset);
    if (index === 1) this.radius = upPoint.add(offset).distanceTo(this.center);
    if (index === 2) this.radius = downPoint.add(offset).distanceTo(this.center);
    if (index === 3) this.radius = leftPoint.add(offset).distanceTo(this.center);
    if (index === 4) this.radius = rightPoint.add(offset).distanceTo(this.center);
    return true;
  }
}


// 两点确定圆
const drawCircleAtTwoPoints = async () => {
  const getPoint = new MrxDbgUiPrPoint();
  const circle = new MxDbCircle();
  // 设置图层为“Comment”
  circle.layer = "Comment";
  const center = await getPoint.go();
  if (!center) {
    throw new Error("Failed to get a valid point for the circle center.");
  }
  circle.center = center;

  // 设置用户绘制回调
  getPoint.setUserDraw((currentPoint, pWorldDraw) => {
    if (!currentPoint) return;
    circle.radius = circle.center.distanceTo(currentPoint);
    pWorldDraw.drawCustomEntity(circle);
    pWorldDraw.drawLine(circle.center, currentPoint);
  });

  // 获取第二个点确定半径
  const point = await getPoint.go();
  if (!point) {
    throw new Error("Failed to get a valid point.");
  }
  circle.radius = circle.center.distanceTo(point);

  // 添加圆到绘图
  MxFun.getCurrentDraw().addMxEntity(circle);
};


export { drawCircleAtTwoPoints };
